//
//  ChartCellFrame.m
//  气泡
//
//  Created by zzy on 14-5-13.
//  Copyright (c) 2014年 zzy. All rights reserved.
//
#define kIconMarginX 9
#define kIconMarginY 5

#define kIconTimeAuserNameOffset  5

#import "ChartCellFrame.h"
#import <CoreFoundation/CoreFoundation.h>
#import "UIImage+RelevantWithImage.h"

@implementation ChartCellFrame

-(void)setChartMessage:(ChartMessage *)chartMessage
{
    _chartMessage = chartMessage;
    
    CGSize winSize = [UIScreen mainScreen].bounds.size;
    CGFloat iconX = kIconMarginX;
    CGFloat iconY = kIconMarginY;
    CGFloat iconWidth = 40;
    CGFloat iconHeight = 40;
    CGFloat timeAuserNameX = iconX + iconWidth + kIconTimeAuserNameOffset;
    
    //消息时间和用户名称
    timeAuserNameX = timeAuserNameX + kIconMarginX;
    if ([_chartMessage.time isEqualToString:@""]) {
        self.timeAnduserNameRect = CGRectMake(0, iconY +10, kScreenBoundWidth, 0);
    }
    else
    {
        self.timeAnduserNameRect = CGRectMake(0, iconY +10, kScreenBoundWidth, 20);
    }
    CGFloat contentY = CGRectGetMaxY(self.timeAnduserNameRect);
    //设置Icon头像的宽高大小
    self.iconRect = CGRectMake(iconX, contentY, iconWidth, iconHeight);
    if(chartMessage.messageType == kMessageFrom)
    {
        //设置Icon头像的宽高大小
        self.iconRect = CGRectMake(iconX, contentY, iconWidth, iconHeight);
        if (chartMessage.isGroupMsg) {
            self.groupOtherNameRect = CGRectMake(iconX + iconWidth + 5, contentY, 100, iconWidth/2);        }
        
    }else{
        //设置Icon头像的宽高大小
        self.iconRect = CGRectMake(kScreenBoundWidth - iconWidth - iconX, contentY, iconWidth, iconHeight);
    }
    //消息内容
    CGFloat contentX = CGRectGetMaxX(self.iconRect)+5;
    CGSize contentSize;
    if (chartMessage.messageStyle == kMessageText) {
        
        NSDictionary *attributes = @{NSFontAttributeName: [UIFont systemFontOfSize:16.2]};
        
        CGSize textContentSize =[chartMessage.content boundingRectWithSize:CGSizeMake(200, MAXFLOAT) options: NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attributes context:nil].size;
        contentSize = CGSizeMake(textContentSize.width + 29, chartMessage.height + 20);
        
    }else if (chartMessage.messageStyle == kMessageImage){
        
        contentSize = CGSizeMake(80 + 2 + 7, 80 + 2 + 2+40);

        
    }else if (chartMessage.messageStyle == kMessageAudio){
        //NSString *contentStr =[NSString stringWithFormat:@"%ld''",(long)chartMessage.duration];

        contentSize = CGSizeMake(13 + 50 + 29, 20 + 24);
    }else if (chartMessage.messageStyle == kMessageVideo){
        
        contentSize = CGSizeMake(80 + 2 + 7, 80 + 2 + 2);
        
    }else if (chartMessage.messageStyle == kMessageCustom){
        contentSize = CGSizeMake(winSize.width - (iconWidth + kIconMarginX)*2 - kIconMarginX *2, 70);
    }
    
    
    
    
    if(chartMessage.messageType == kMessageTo)
    {
       contentX = winSize.width - kIconMarginX - contentSize.width - self.iconRect.size.width - 5;
    }
    
  
    self.chartViewRect = CGRectMake(contentX, contentY, contentSize.width, contentSize.height);
    if (chartMessage.isGroupMsg) {
        if (chartMessage.messageType == kMessageFrom) {
            self.chartViewRect = CGRectMake(contentX, contentY + iconWidth/2, contentSize.width, contentSize.height);
        }
    }
    self.unReadRect = CGRectMake(contentX + kIconMarginX + contentSize.width, contentY, 10, 10);
    self.sendStatusImageViewRect = CGRectMake(contentX - 20, (contentSize.height - 15)/2 + contentY, 15, 15);
    
    
    self.cellHeight = MAX(CGRectGetMaxY(self.iconRect), CGRectGetMaxY(self.chartViewRect))+kIconMarginX;
}

@end
